package generic;

import java.util.*;

/**
 * @author DCSGO
 * @version 1.0
 * 泛型使用举例
 */

@SuppressWarnings({"all"})
public class GenericExercise {
    //1.创建3个学生对象
    //2.放入到 HashSet 中学生对象 使用泛型
    //3.放入到 HashMap 中要求使用泛型
    //  Key 是 String name Value 就是学生对象
    //4.使用两种方式遍历
    public static void main(String[] args) {
        Student stu1 = new Student("王强", 18);
        Student stu2 = new Student("马哲平", 19);
        Student stu3 = new Student("王小明", 17);

        HashSet<Student> hashSet = new HashSet<Student>();
        hashSet.add(stu1);
        hashSet.add(stu2);
        hashSet.add(stu3);

        System.out.println("---foreach---");
        for (Student student : hashSet) {
            System.out.println(student);
        }
        //---foreach---
        //Student{name='王小明', age=17}
        //Student{name='王强', age=18}
        //Student{name='马哲平', age=19}

        System.out.println("---Iterator---");
        Iterator<Student> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            System.out.println(student);
        }
        //---Iterator---
        //Student{name='王小明', age=17}
        //Student{name='王强', age=18}
        //Student{name='马哲平', age=19}

        HashMap<String, Student> hashMap = new HashMap<String, Student>();
        hashMap.put(stu1.name, stu1);
        hashMap.put(stu2.name, stu2);
        hashMap.put(stu3.name, stu3);

        System.out.println("---KeySetforeach---");
        Set<String> keySet = hashMap.keySet();
        for (String name : keySet) {
            System.out.println(name + "-" + hashMap.get(name));
        }
        //---KeySetforeach---
        //王强-Student{name='王强', age=18}
        //马哲平-Student{name='马哲平', age=19}
        //王小明-Student{name='王小明', age=17}

        System.out.println("---EntrySetIterator---");
        Set<Map.Entry<String, Student>> entrySet = hashMap.entrySet();
        Iterator<Map.Entry<String, Student>> iterator1 = entrySet.iterator();
        while (iterator1.hasNext()) {
            Map.Entry<String, Student> elem = iterator1.next();
            System.out.println(elem.getKey() + "-" + elem.getValue());
        }
        //---EntrySetIterator---
        //王强-Student{name='王强', age=18}
        //马哲平-Student{name='马哲平', age=19}
        //王小明-Student{name='王小明', age=17}
    }
}

class Student {
    String name;
    int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
